{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2e92e413",
   "metadata": {},
   "source": [
    "### P087 - 邮件文本分类 - 加载文本数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "cf5a0bae",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e85f260b",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_train = pd.read_csv(\"email_data_train.csv\")\n",
    "target_train = pd.read_csv(\"email_target_train.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f3ec82ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['text'], dtype='object')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_train.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "868ee529",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"From: stephens@geod.emr.ca (Dave Stephenson)\\r\\nSubject: Re: Clementine Science Team Selected\\r\\nNntp-Posting-Host: ngis.geod.emr.ca\\r\\nOrganization: Dept. of Energy, Mines, and Resources, Ottawa\\r\\nLines: 32\\r\\n\\r\\nnickh@CS.CMU.EDU (Nick Haines) writes:\\r\\n\\r\\n>In article <stephens.734792933@ngis> stephens@geod.emr.ca (Dave Stephenson) writes:\\r\\n\\r\\n>   Remember the first government scientist in the British Empire was\\r\\n>   the Astronomer Royal, who was paid [...] from the Department\\r\\n>   of Ordinance Budget (i.e. the military). Flamsteed House (the original\\r\\n>   RGO) was built out of Army Surplus Scrap ( A gate house at the Tower of\\r\\n>   London ?), and paid for by the sale of time expired gunpowder [...]\\r\\n\\r\\n>At the time, astronomy was vital to the military, in that navigation\\r\\n>and cartography were of primary impoortance to the military, and good\\r\\n>cartography was impossible without good astronomy.\\r\\n\\r\\n>The relevance these daysis somewhat less obvious.\\r\\n\\r\\n>Nick\\r\\n\\r\\nIt still applies, except the astronomy these days is Very Long Baseline\\r\\nRadio Astronomy coupled to GPS and Satellite Laser Ranging. The data\\r\\nfrom NASA's and the Naval Observatory's (among others) is a vital\\r\\nsource of data for studies into crustal dynamics, Earth rotation, and\\r\\npurturbations. Every time there is a leap second added to the New Year,\\r\\nremember the military and science are still co-habiting nicely. The\\r\\nsame VLBI was used to track Gallileo as it passed the Earth, and used\\r\\nso little fuel that it can afford to observe Ida.\\r\\n\\r\\n--\\r\\nDave Stephenson\\r\\nGeodetic Survey of Canada\\r\\nOttawa, Ontario, Canada\\r\\nInternet: stephens@geod.emr.ca\\r\\n\""
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_train[\"text\"][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "faae7b53",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['target'], dtype='object')"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "target_train.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "090e5a7b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "target_train[\"target\"][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fbeb7409",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c6d25e2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "623b1459",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "341b4eb9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "03ed82ee",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e6f1e0de",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7a6d76ff",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "c117fdba",
   "metadata": {},
   "source": [
    "### P088 - 邮件文本分类 - 输入数据转换成list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7ed9465d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>text</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>From: ab@nova.cc.purdue.edu (Allen B)\\r\\nSubje...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>From: stephens@geod.emr.ca (Dave Stephenson)\\r...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>From: dotzlaw@ccu.umanitoba.ca (Helmut Dotzlaw...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>From: flb@flb.optiplan.fi (\"F.Baube[tm]\")\\r\\nS...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>From: cchung@sneezy.phy.duke.edu (Charles Chun...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                text\n",
       "0  From: ab@nova.cc.purdue.edu (Allen B)\\r\\nSubje...\n",
       "1  From: stephens@geod.emr.ca (Dave Stephenson)\\r...\n",
       "2  From: dotzlaw@ccu.umanitoba.ca (Helmut Dotzlaw...\n",
       "3  From: flb@flb.optiplan.fi (\"F.Baube[tm]\")\\r\\nS...\n",
       "4  From: cchung@sneezy.phy.duke.edu (Charles Chun..."
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "57f52cee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>target</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   target\n",
       "0       0\n",
       "1       1\n",
       "2       0\n",
       "3       1\n",
       "4       1"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "target_train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "140bf5cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_train = data_train[\"text\"].tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "b1d6f297",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(data_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "bbbccad2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "50"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(data_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "aa6d4a3b",
   "metadata": {},
   "outputs": [],
   "source": [
    "target_train = target_train[\"target\"].tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "fba8c352",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(target_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "ded9e691",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "50"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(target_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b9abebd0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b3dc6fc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4aa743aa",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "577cb872",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a42a4147",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "a242ac0a",
   "metadata": {},
   "source": [
    "### P089 - 邮件文本分类 - 使用计数器向量化文本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "c27231b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.feature_extraction.text import CountVectorizer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "beade806",
   "metadata": {},
   "outputs": [],
   "source": [
    "vectorizer = CountVectorizer()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "2ec7e3c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(data_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "755c004c",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_train_vectorized = vectorizer.fit_transform(data_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "616f21f9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "scipy.sparse.csr.csr_matrix"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(data_train_vectorized)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "ef2adc4a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(50, 3225)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_train_vectorized.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "3cf9321c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3225"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(vectorizer.get_feature_names())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "0c5650f8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['diving', 'division', 'dizzying', 'dkauni2', 'dmorf', 'dmorfx', 'dmorph', 'dnw', 'do', 'documentation', 'does', 'doesn', 'doing', 'dollars', 'domain', 'dominik', 'don', 'donations', 'done', 'dos', 'dotzlaw', 'double', 'douglas', 'down', 'downloaded', 'downsizing', 'downwards', 'dr', 'drawn', 'drive', 'driver', 'dropped', 'dry', 'dryden', 'dseg', 'dsp', 'dt', 'dta', 'dtax', 'dtek', 'du', 'duc', 'due', 'duke', 'dumb', 'dundee', 'dunn', 'durham', 'during', 'durned', 'dutch', 'dux', 'dwestner', 'dxf', 'dynamic', 'dynamically', 'dynamics', 'each', 'earlham', 'earlier', 'early', 'earth', 'easier', 'easily', 'easy', 'eberhart', 'economically', 'ecpdsharmony', 'ed', 'edge', 'edged', 'edges', 'edimg', 'edinburgh', 'edition', 'ednobles', 'edu', 'educate', 'education', 'educational', 'edward', 'edwards', 'efectively', 'efficiency', 'efficient', 'efforts', 'either', 'electronic', 'electronics', 'else', 'elson', 'elvis', 'email', 'embarrassed', 'emory', 'empire', 'emr', 'enclosed', 'encod', 'encode']\n"
     ]
    }
   ],
   "source": [
    "print(vectorizer.get_feature_names()[1000:1100])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7590c10c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4eca2755",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20c53af2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c034ff68",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "16c7ac45",
   "metadata": {},
   "source": [
    "### P090 邮件文本分类 - 计数器数据训练分类模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "938aed91",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.naive_bayes import MultinomialNB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "7340951d",
   "metadata": {},
   "outputs": [],
   "source": [
    "classifier = MultinomialNB()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "755b9cca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MultinomialNB()"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classifier.fit(data_train_vectorized, target_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "1af01cd9",
   "metadata": {},
   "outputs": [],
   "source": [
    "docs = ['The graphic designer requires a good processor to work', \n",
    "        'Flights into space']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "a4112749",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_new = vectorizer.transform(docs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "c09631b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_pred = classifier.predict(data_new)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "e6a5b489",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('The graphic designer requires a good processor to work', 0),\n",
       " ('Flights into space', 1)]"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(zip(docs, data_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a13cb3ef",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a1397011",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bba08334",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5124635b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7bbca3f4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d09a1456",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14293a6d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "f7cf08b5",
   "metadata": {},
   "source": [
    "### P091 邮件文本分类 - 使用TFIDF向量化文本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "540325a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.feature_extraction.text import TfidfVectorizer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "83f488af",
   "metadata": {},
   "outputs": [],
   "source": [
    "vectorizer = TfidfVectorizer()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "67aed9eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_train_vectorized = vectorizer.fit_transform(data_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "4e27253f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(50, 3225)"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_train_vectorized.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "29918ace",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[0., 0., 0., ..., 0., 0., 0.]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_train_vectorized[1].todense()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "394cd4ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['diving',\n",
       " 'division',\n",
       " 'dizzying',\n",
       " 'dkauni2',\n",
       " 'dmorf',\n",
       " 'dmorfx',\n",
       " 'dmorph',\n",
       " 'dnw',\n",
       " 'do',\n",
       " 'documentation',\n",
       " 'does',\n",
       " 'doesn',\n",
       " 'doing',\n",
       " 'dollars',\n",
       " 'domain',\n",
       " 'dominik',\n",
       " 'don',\n",
       " 'donations',\n",
       " 'done',\n",
       " 'dos',\n",
       " 'dotzlaw',\n",
       " 'double',\n",
       " 'douglas',\n",
       " 'down',\n",
       " 'downloaded',\n",
       " 'downsizing',\n",
       " 'downwards',\n",
       " 'dr',\n",
       " 'drawn',\n",
       " 'drive',\n",
       " 'driver',\n",
       " 'dropped',\n",
       " 'dry',\n",
       " 'dryden',\n",
       " 'dseg',\n",
       " 'dsp',\n",
       " 'dt',\n",
       " 'dta',\n",
       " 'dtax',\n",
       " 'dtek',\n",
       " 'du',\n",
       " 'duc',\n",
       " 'due',\n",
       " 'duke',\n",
       " 'dumb',\n",
       " 'dundee',\n",
       " 'dunn',\n",
       " 'durham',\n",
       " 'during',\n",
       " 'durned',\n",
       " 'dutch',\n",
       " 'dux',\n",
       " 'dwestner',\n",
       " 'dxf',\n",
       " 'dynamic',\n",
       " 'dynamically',\n",
       " 'dynamics',\n",
       " 'each',\n",
       " 'earlham',\n",
       " 'earlier',\n",
       " 'early',\n",
       " 'earth',\n",
       " 'easier',\n",
       " 'easily',\n",
       " 'easy',\n",
       " 'eberhart',\n",
       " 'economically',\n",
       " 'ecpdsharmony',\n",
       " 'ed',\n",
       " 'edge',\n",
       " 'edged',\n",
       " 'edges',\n",
       " 'edimg',\n",
       " 'edinburgh',\n",
       " 'edition',\n",
       " 'ednobles',\n",
       " 'edu',\n",
       " 'educate',\n",
       " 'education',\n",
       " 'educational',\n",
       " 'edward',\n",
       " 'edwards',\n",
       " 'efectively',\n",
       " 'efficiency',\n",
       " 'efficient',\n",
       " 'efforts',\n",
       " 'either',\n",
       " 'electronic',\n",
       " 'electronics',\n",
       " 'else',\n",
       " 'elson',\n",
       " 'elvis',\n",
       " 'email',\n",
       " 'embarrassed',\n",
       " 'emory',\n",
       " 'empire',\n",
       " 'emr',\n",
       " 'enclosed',\n",
       " 'encod',\n",
       " 'encode']"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vectorizer.get_feature_names()[1000:1100]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9c4cc15d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb5ee177",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8f0d088",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a10cf8ce",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94accd50",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "109e5d7e",
   "metadata": {},
   "source": [
    "### P092 邮件文本分类 - TFIDF数据训练分类模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "fa590d68",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.naive_bayes import MultinomialNB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "f83ac1a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "classifier = MultinomialNB()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "6379e614",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MultinomialNB()"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classifier.fit(data_train_vectorized, target_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "68fa6a6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "docs = ['The graphic designer requires a good processor to work', \n",
    "        'Flights into space']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "617f880a",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_new = vectorizer.transform(docs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "1afe1e90",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "sklearn.feature_extraction.text.TfidfVectorizer"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(vectorizer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "ee501d76",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_pred = classifier.predict(data_new)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "8081e2d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('The graphic designer requires a good processor to work', 0),\n",
       " ('Flights into space', 1)]"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(zip(docs, data_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c0cc877f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18dd0388",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
